// url_test.ts - 测试登录API中的URL构建

function testUrlConstruction() {
  console.log("====== URL 构建测试 ======");
  
  // 模拟请求URL
  const testUrls = [
    "http://localhost:8000/api/auth/login", 
    "http://localhost:8000/api/auth/login?redirect=/admin/posts"
  ];
  
  for (const testUrl of testUrls) {
    console.log(`\n测试URL: ${testUrl}`);
    
    try {
      // 构建URL对象
      const url = new URL(testUrl);
      
      // 提取当前URL的信息
      console.log("URL信息:");
      console.log("- origin:", url.origin);
      console.log("- pathname:", url.pathname);
      console.log("- searchParams:", Object.fromEntries(url.searchParams.entries()));
      
      // 测试构建登录页面URL
      const loginUrl = new URL("/admin/login", url.origin);
      console.log("\n登录页面URL (origin):", loginUrl.toString());
      
      // 测试添加参数
      loginUrl.searchParams.set("error", "测试错误消息");
      console.log("带错误参数的URL:", loginUrl.toString());
      
      // 测试成功重定向URL
      const redirectPath = url.searchParams.get("redirect") || "/";
      const successRedirectUrl = new URL(redirectPath, url.origin);
      console.log("\n成功重定向URL:", successRedirectUrl.toString());
      
      // 测试添加success参数
      successRedirectUrl.searchParams.set("login_success", "true");
      console.log("带success参数的重定向URL:", successRedirectUrl.toString());
    } catch (error) {
      console.error(`❌ 处理URL "${testUrl}" 时出错:`, error);
    }
  }
}

// 运行测试
if (import.meta.main) {
  testUrlConstruction();
} 